<?php
/**
 * Created by PhpStorm.
 * User: 秋刀鱼
 * Date: 2018/7/30
 * Time: 14:13
 */

namespace app\models;

use yii\base\Model;
use Yii;
use \common\helps\tools;//使用工具类

class SpecialProject extends Model
{
    //表名
    protected $table = 'special_project';

    /**
     * title 根据名称查找上级id
     * @param $project_name
     * @return array|bool
     */
    public function ParentProject($project_name)
    {
        $query = new \yii\db\Query();
        $name = trim($project_name,'');
        $special_project = $query->select(['id','project_id','total_money','pid'])->from('data_yii.special_project_child')->where(['project_name' =>$name ])->one();

        return $special_project;
    }

    /**
     * title 根据id查找上级id
     * @param $project_name
     * @return array|bool
     */
    public function ParentId($id)
    {
        $query = new \yii\db\Query();
        $special_project = $query->select(['id','project_id','total_money','pid'])->from('data_yii.special_project_child')->where(['project_id' => $id,'pid'=>0])->one();

        return $special_project;
    }

    /**
     * title 检查 名字 所属单位是否存在
     * @param $name
     * @param $belong_company
     * @return array|bool
     */
    public function checkRepetition($name,$belong_company)
    {
        $query = new \yii\db\Query();
        $child = $query->select(['id'])->from('data_yii.special_project_child')->where(['project_name' => $name,'belong_company' => $belong_company ])->one();
        return $child;

    }

    /**
     * title 添加
     * @param $create_time
     * @return int
     * @throws \yii\db\Exception
     */
    public function addProject($create_time,$uid)
    {
        $id = tools::createUuid();
        $field = ['id','create_time','create_id'];
        $batchData = [[$id,$create_time,$uid]];
        $add = Yii::$app->db->createCommand()->batchInsert($this->table,$field,$batchData)->execute();
        return $add;
    }

    /**
     * 获取最后一条id
     * @return array|bool
     */
    public function lastId(){
        $query = new \yii\db\Query();
        $last = $query->select(['id'])->from('data_yii.special_project')->orderBy('create_time DESC')->limit(1)->one();

        return $last;
    }




    /**
     * title 修改项目主表project
     * @param $project_type
     * @param $begin_time
     * @param $end_time
     * @param $budget_money
     * @param $grant_way
     * @param $create_time
     * @param $type
     * @throws \yii\db\Exception
     */
    public function saveProject($id,$project_type,$begin_time,$end_time,$grant_way,$update_time,$type,$create_id,$status)
    {
        $data = ['id'=>$id,'project_type'=>$project_type,'begin_time'=>$begin_time,'end_time'=>$end_time,'grant_way'=>$grant_way,'update_time'=>$update_time,'type'=>$type,'create_id'=>$create_id,'status'=>$status];
//        var_dump($data);die;
        $save = Yii::$app->db->createCommand()->update('data_yii.special_project', $data, "id = '$id'")->execute();
        return $save;

    }


    /**
     * title 详情
     * @param $id
     * @return array|bool
     */
    public static function detail($id,$status)
    {
        $query =  new \yii\db\Query();
        $list = $query->select(['a.id','a.project_type','a.begin_time','a.end_time','a.grant_way','a.type as main_type','b.item_no','b.project_principal','b.total_money','b.project_name','b.belong_company','b.pid'])
            ->from('data_yii.special_project_child AS b')
            ->rightJoin('data_yii.special_project AS a', 'b.project_id = a.id')
            ->where(['a.id' => $id,'a.status'=>$status])
            ->one();
        if($list['pid'] != 0) {
            $special = new SpecialProject;
            $pidChild = $special->pidParent($list['pid']);
            $list['parent_name'] = $pidChild['project_name'];
        }else{
            $list['parent_name'] = '';
        }

        return $list;
    }


    /**
     * title 查找子级列表
     * @param $pid
     * @return array
     */
    public function pidParent($pid)
    {
        $query =  new \yii\db\Query();
        $result = $query->select('*')->from('data_yii.special_project_child')->where(['id' => $pid])->one();

        return $result;
    }


    /**
     * title 添加项目子表project
     * @param $project_id
     * @param $item_no
     * @param $project_principal
     * @param $total_money
     * @param $project_name
     * @param $belong_company
     * @param $longitude
     * @param $latitude
     * @param int $pid
     * @param $create_time
     * @return int
     * @throws \yii\db\Exception
     */
    public function addChild($project_id,$create_time,$item_no = '',$project_principal = '',$total_money = 0,$project_name = '',$belong_company = '',$longitude = '',$latitude = '',$pid = 0)
    {
        $query = new \yii\db\Query();
        $id = tools::createUuid();
        $this->table = 'data_yii.special_project_child';

        //清除之前的
        $child = $query->select(['id'])->from($this->table)->where(['project_id'=>$project_id])->one();
        if($child) {
            $del = Yii::$app->db->createCommand()->delete($this->table, "project_id = '$project_id'")->execute();
            if (!$del) {
                return 0;
            }
        }


        $field = ['id','project_id','item_no','project_principal','total_money','project_name','belong_company','longitude','latitude','pid','create_time'];
        $batchData = [[$id,$project_id,$item_no,$project_principal,$total_money,$project_name,$belong_company,$longitude,$latitude,$pid,$create_time]];
        $result = Yii::$app->db->createCommand()->batchInsert($this->table,$field,$batchData)->execute();



        return $result;

    }

    /**
     * title 查询列表
     * @param $uid
     */
    public function projectList($uid)
    {
        $query =  new \yii\db\Query();
        $list = $query->select(['a.id','a.project_type','a.begin_time','a.end_time','a.create_time','b.item_no','b.belong_company','b.total_money','b.item_no','b.project_name','b.belong_company'])
            ->from('data_yii.special_project_child AS b')
            ->rightJoin('data_yii.special_project AS a', 'b.project_id = a.id')
            ->where(['a.create_id' => $uid,'a.status' => 1])
            ->orderBy('a.create_time DESC')
            ->all();

        return $list;

    }

    /**
     *title 搜索
     */
    public function projectSearch($project_name)
    {
        $query =  new \yii\db\Query();
        $list = $query->select(['a.id','a.project_type','a.begin_time','a.end_time','a.create_time','b.project_name','b.item_no','b.belong_company','b.total_money'])
            ->from('data_yii.special_project_child AS b')
            ->rightJoin('data_yii.special_project AS a', 'b.project_id = a.id')
            ->where(['b.project_name' => $project_name])
            ->orderBy('a.create_time DESC')
            ->all();

        return $list;

    }

    /**
     * title 删除单删
     * @param $id
     * @return int
     * @throws \yii\db\Exception
     */
    public static function delProject($id)
    {
        $query =  new \yii\db\Query();

        //数量表
        $type = $query->select(['id'])->from('data_yii.type_num')->where(['project_id' => $id])->one();
        if($type) {
            $delType = Yii::$app->db->createCommand()->delete('data_yii.type_num', "project_id = '$id'")->execute();
            if (!$delType) {
                return 1;
            }
        }
        //子表
        $child = $query->select(['id'])->from('data_yii.special_project_child')->where(['project_id' => $id])->one();
        if($child) {
            $delChild = Yii::$app->db->createCommand()->delete('data_yii.special_project_child', "project_id = '$id'")->execute();
            if (!$delChild) {
                return 1;
            }
        }

        //主表
        $find = $query->select(['id'])->from('data_yii.special_project')->where(['id' => $id])->one();
        if($find) {
            $del = Yii::$app->db->createCommand()->delete('data_yii.special_project', "id = '$id'")->execute();
            if (!$del) {
                return 1;
            }
        }

        return 2;

    }


    /**
     * title 子项id目查询项目信息
     * @return array|bool
     */
    public static function childProject($child_id)
    {
        $query = new \yii\db\Query();
        $find = $query->select(['a.id','a.project_id','a.item_no','a.project_principal','a.total_money','a.project_name','a.belong_company','a.longitude','a.latitude','b.project_type','b.begin_time','b.end_time','b.grant_way','b.type','b.create_id','b.update_id','b.status'])
            ->from(tools::databaseName(). '.special_project_child as a' )
            ->rightJoin(tools::databaseName(). '.special_project AS b', 'b.id = a.project_id')
            ->where(['a.id'=>$child_id])
            ->one();
        return $find;
    }





}